import dom, { ajax, domg } from "../../init.js";
import { obj } from "../../index.js";
import { userlistinfo } from "../../../util/interface.js";
import event from "./eventhandle.js";

export async function initandmi() {
  const result = await ajax.post("/user/getuserlist");
  const data = JSON.parse(result as string).data as Array<userlistinfo>;
  console.log(data);

  let str = `
  <h2>index: 管理</h2>
  <br>
  <hr>
  <ul class="userlist">

  </ul>`;
  dom.content.content.innerHTML = str;
  data.forEach((item, index) => {
    if (index === 0) return;
    let lis = `
    <li index=${index} key = ${item.id}>
      用户名:<span class="andmiUserName">${item.name}</span>
      用户昵称:<span class="andmiNickName">${item.nickName}</span>
      是否为VIP: <input type="checkbox" ${
        item.right == "2" ? 'checked=""' : ""
      } class="andmiUserRight">
      <button class="andmiUserBtn">保存</button>
    </li>`;
    domg(".userlist").insertAdjacentHTML("beforeend", lis);
    const li = domg(".userlist").children[index - 1] as HTMLElement;
    (li.querySelector(".andmiUserBtn") as HTMLElement).onclick = event.clickbtn;
  });
}
